""" Input mode to plot a course """
import math

from pygame.locals import *
import pygame

from pgEngine.directions import ArbitraryDirection
from pgEngine import movementData, boat
from pgEngine.point import Point

from colors import *

class PlotCourseMode:
    def __init__(self, game, window, ship):
        self.game = game
        self.window = window
        self.map = window.map

        # Set the source
        self.boat = ship
        self.origin = self.game.ocean.getCoord(ship) + Point(0.5, -0.5)
        self.sourcePos = self.origin
        
        if len(ship.plot.data):
            ship.owner.onBoatRestarted(boat)
            for leg in ship.plot.data:
                delta = Point.fromVector(leg.dir.angle, leg.dist)            
                self.sourcePos += delta
    
    def onEvent(self, event):
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.window.mode.remove(self)
                if len(self.boat.plot.data) > 0:
                    self.boat.finishTurn()
                return False
            
        elif self.window.checkOverlayClick(event.pos):
            
            if event.type == MOUSEBUTTONDOWN:

                if event.button == 1:
                    return False
            
            elif event.type == MOUSEBUTTONUP:

                if event.button == 1:
                    # Plot the course.
                    sourcePos = self.sourcePos
                    targetPos = self.map.getMousePos()
                    targetPos = Point(*targetPos)
                    delta = targetPos - sourcePos
        
                    angle = math.atan2(delta.y, delta.x)
                    if angle < 0:
                        angle += math.pi * 2
                    ndir = ArbitraryDirection(radians=angle)
                    dist = math.hypot(delta.x,delta.y)
                    if dist < 1.0:
                        # All done plotting legs.
                        self.window.mode.remove(self)
                        if len(self.boat.plot.data) > 0:
                            self.boat.finishTurn()
                        return False
        
                    segment = movementData.move(ndir, dist)
                    try:
                        self.boat.plot.append(segment)
                    except boat.MovementException:
                        self.window.dialogMessage(
                            "Plotted course exceeds maximum distance."
                            )
                    else:
                        self.window.dialogMessage(
                            "Course leg plotted %s." % segment
                            )
                        self.sourcePos = targetPos
    
                    return False
                
                elif event.button == 3:
                    # Stop plotting.
    
                    self.window.mode.pop()
                    if len(self.boat.plot.data) > 0:
                        self.boat.finishTurn()
                    return False
                
                
                
    def drawLeg(self, surface, sourcePos, targetPos, lastleg=False):
        if lastleg:
            lcolor = dkgrey
            source = self.map.getScreenPos(sourcePos)
            target = targetPos
        else:
            source = self.map.getScreenPos(sourcePos)
            target = self.map.getScreenPos(targetPos)
            lcolor = ltgrey
            

        # Draw the connector
        pygame.draw.aaline(surface, lcolor, source.pos, target.pos, 1)
        
        # Draw the source
        pygame.draw.circle(surface, lcolor, source.pos, 4, 0)

        # Draw the target.
        pygame.draw.circle(surface, white, target.pos, 4, 0)
        pygame.draw.circle(surface, lcolor, target.pos, 5, 1)
        if lastleg:
            pygame.draw.circle(surface, lcolor, target.pos, 10, 1)
            pygame.draw.circle(surface, lcolor, target.pos, 10, 2)
        
    def onDrawMapObjects(self, surface):
        sourcePos = self.origin
        for leg in self.boat.plot.data:
            delta = Point.fromVector(leg.dir.angle, leg.dist)            
            targetPos = sourcePos + delta
            self.drawLeg(surface, sourcePos, targetPos, False)
            sourcePos = targetPos
        
        screenPos = Point(self.window.mouseX, self.window.mouseY)
        targetPos = screenPos - Point(*self.map.rcMap.topleft)
        self.drawLeg(surface, sourcePos, targetPos, True)
